#include "SipSubB2bSession.hpp"

namespace wtsip{

CSipSubB2bSession::CSipSubB2bSession(CSipGDService& srv, 
                                     CSipSubscribeSession2& caller, 
                                     CSipSubscribeSession2& callee):
CSipB2BSession(srv, caller, callee)
{}


CSipSubB2bSession::~CSipSubB2bSession(void)
{}


bool CSipSubB2bSession::startCaller(SipMessage& calleeReq)
{
    CSipSubscribeSession2* caller = dynamic_cast<CSipSubscribeSession2*>(&m_caller);
    if (caller)
    {
        std::string body;
        if (calleeReq.getContents()){
            body = calleeReq.getContents()->getBodyData().c_str();
        }
        resip::Data callerId = caller->subscribe(body);
        insertMap(callerId, calleeReq.getTransactionId());
    }
    return true;
}


bool CSipSubB2bSession::insertMap(const resip::Data& callerId, const resip::Data& calleeId)
{
    m_transMap.insert(std::pair<resip::Data, resip::Data>(callerId, calleeId));
    return true;
}


const resip::Data& CSipSubB2bSession::getCalleeId(const resip::Data& callerId)
{
    std::map<resip::Data, resip::Data>::iterator it;
    it = m_transMap.find(callerId);
    if (it != m_transMap.end()){
        return it->second;
    }
    return resip::Data::Empty;
}


/////////////////////  subscribe handle  ///////////////////

bool CSipSubB2bSession::onSubscribe(SipMessage& msg, CSipSubscribeSession2& subs)
{
    CSipSubscribeSession2* caller = dynamic_cast<CSipSubscribeSession2*>(&m_caller);
    if (caller)
    {
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        resip::Data callerId = caller->subscribe(body);
        insertMap(callerId, msg.getTransactionId());
    }
    return true;
}


bool CSipSubB2bSession::onUnsubscribe(SipMessage& msg, CSipSubscribeSession2& subs)
{
    CSipSubscribeSession2* caller = dynamic_cast<CSipSubscribeSession2*>(&m_caller);
    if (caller)
    {
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        resip::Data callerId = caller->unsubscribe(body);
        insertMap(callerId, msg.getTransactionId());
    }
    return true;
}


bool CSipSubB2bSession::onNotify(SipMessage& msg, CSipSubscribeSession2& subs)
{
    CSipSubscribeSession2* callee = dynamic_cast<CSipSubscribeSession2*>(&m_callee);
    if (callee)
    {
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        resip::Data callerId = callee->notify(body);
        insertMap(callerId, msg.getTransactionId());
    }
    return true;
}


////////////////////   Session handle //////////////////////

bool CSipSubB2bSession::onSuccess(SipMessage& msg, CSipSessionBase& sb){
    CSipSessionBase& wkSession = (&sb != &m_callee)? m_callee: m_caller;
    CSipSubscribeSession2* wkSubs = dynamic_cast<CSipSubscribeSession2*>(&wkSession);

    if (wkSubs)
    {
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        const resip::Data& calleeId = getCalleeId(msg.getTransactionId());
        wkSubs->replyOK(calleeId, body);
    }
    return true;
}


bool CSipSubB2bSession::onFailed(SipMessage& msg, CSipSessionBase& sb)
{
    CSipSessionBase& wkSession = (&sb != &m_callee)? m_callee: m_caller;
    CSipSubscribeSession2* wkSubs = dynamic_cast<CSipSubscribeSession2*>(&wkSession);

    if (wkSubs)
    {
        const resip::Data& calleeId = getCalleeId(msg.getTransactionId());
        int code = msg.header(h_StatusLine).responseCode();
        wkSubs->replyFail(calleeId, CSipSessionBase::getReasonbyCode(code));
    }
    return true;
}


bool CSipSubB2bSession::onTrying(SipMessage& msg, CSipSessionBase& sb){
    return true;
}


bool CSipSubB2bSession::onDestroy(CSipSessionBase& sb){
    return destroy();
}


}//namespace

